package com.wjf.accesscontrol.ui.read.dialog

import android.os.Bundle
import android.os.CountDownTimer
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wjf.accesscontrol.R
import com.wjf.accesscontrol.base.MVVMBaseDialogFragment
import com.wjf.accesscontrol.databinding.DialogResultBinding
import com.wjf.accesscontrol.ui.TitleBarActivity
import com.wjf.accesscontrol.ui.TitleBarViewModel
import com.wjf.accesscontrol.util.COMMON_FLAG
import com.wjf.accesscontrol.util.COMMON_FLAG_MSG

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2024/1/30 10:05
 *
 */
/**
 * 1: 读卡成功
 * 2: 读卡失败
 * 3: 登录错误
 * 4: 激活成功
 */
const val DIALOG_TYPE_1 = "1"
const val DIALOG_TYPE_2 = "2"
const val DIALOG_TYPE_3 = "3"
const val DIALOG_TYPE_4 = "4"
class ResultDialog(private var resultDialogCallback: ResultDialogCallback?): MVVMBaseDialogFragment<TitleBarViewModel,DialogResultBinding>() {

    private var timer: CountDownTimer? = null
    private var showTime = 3
    private lateinit var type: String

    companion object{
        fun newInstance(type: String, resultDialogCallback: ResultDialogCallback?, msg: String = ""): ResultDialog{
            val resultDialog = ResultDialog(resultDialogCallback)
            val bundle = Bundle()
            bundle.putString(COMMON_FLAG,type)
            bundle.putString(COMMON_FLAG_MSG,msg)
            resultDialog.arguments = bundle
            return resultDialog
        }
    }

    override fun initViewModel() = (activity as TitleBarActivity).vm

    override fun initWidth() = resources.getDimension(R.dimen.dp456).toInt()

    override fun initHeight(): Int{
        return when(type){
            DIALOG_TYPE_3 -> { resources.getDimension(R.dimen.dp324).toInt() }
            else ->{ resources.getDimension(R.dimen.dp284).toInt() }
        }
    }

    override fun initView() {
        type = "${arguments?.getString(COMMON_FLAG)}"
        when(type){

            DIALOG_TYPE_1 -> {
                showTime = 1
                binding.ivIcon.setImageResource(R.mipmap.success)
                binding.tvTip.text = mView.resources.getString(R.string.tip_success)
                binding.tvTip2.visibility = View.GONE
            }
            DIALOG_TYPE_2 -> {
                showTime = 1
                binding.ivIcon.setImageResource(R.mipmap.fail)
                binding.tvTip.text = mView.resources.getString(R.string.tip_fail)
                binding.tvTip2.visibility = View.GONE
            }

            DIALOG_TYPE_3 -> {
                showTime = 3
                binding.ivIcon.setImageResource(R.mipmap.fail)
                binding.tvTip.text = "${arguments?.getString(COMMON_FLAG_MSG)}"
                binding.tvTip2.text = mView.resources.getString(R.string.login_deny_tip_2)
            }
            DIALOG_TYPE_4 -> {
                showTime = 3
                binding.ivIcon.setImageResource(R.mipmap.success)
                binding.tvTip.text = "${arguments?.getString(COMMON_FLAG_MSG)}"
                binding.tvTip2.visibility = View.GONE
            }
        }
    }
    override fun initData() {
        countDownTimer()
    }

    private fun countDownTimer() {
        timer?.cancel()

        timer = object : CountDownTimer((showTime) * 1000L, 1000L) {

            override fun onTick(millisUntilFinished: Long) {}

            override fun onFinish() {
                if (dialog != null){
                    dismiss()
                }
                resultDialogCallback?.dismiss()

            }
        }
        timer?.start()
    }

    override fun initViewBinding(inflater: LayoutInflater, container: ViewGroup?) =
        DialogResultBinding.inflate(inflater,container,false)


}